package com.longe.esaypoi.exportexcel;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TwoSimpleDemo {

    public static void main(String[] args) throws Exception {

        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        ExcelExportEntity excelEntity = new ExcelExportEntity("班级", "class");

        /**向下合并单元格**/
        excelEntity.setNeedMerge(true);

        entity.add(excelEntity);
        excelEntity = new ExcelExportEntity("学生", "students");
        //excelEntity = new ExcelExportEntity(null, "students");
        List<ExcelExportEntity> temp = new ArrayList<>();
        temp.add(new ExcelExportEntity("姓名", "name"));
        temp.add(new ExcelExportEntity("学号", "no"));
        excelEntity.setList(temp);
        entity.add(excelEntity);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 1; i < 4; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("class", "班级_" + i);

            List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", "姓名" + i*10);
            map1.put("no", "学号" + i*10);
            tempList.add(map1);
            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", "姓名" + i*20);
            map2.put("no", "学号" + i*20);
            tempList.add(map2);
            map.put("students", tempList);

            list.add(map);
        }

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "firstSheet"), entity,
                list);

        FileOutputStream fos = new FileOutputStream("D:\\TEST_EASY_POI\\EXPORT\\twoTestExport.xlsx");
        workbook.write(fos);
        fos.close();
    }
}
